﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;
using RedemptoristRetreatCenter.DatabaseUtility;
using RedemptoristRetreatCenter.HelperUtility;

namespace RedemptoristRetreatCenter
{
    public partial class RetreatAttendees : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataTable retreatAttendanceTable = new DataTable();
                DataRow tableRow = null;
                
                retreatAttendanceTable.Columns.Add(new DataColumn("Retreat Title", typeof(string)));
                retreatAttendanceTable.Columns.Add(new DataColumn("Attendee Name", typeof(string)));
                retreatAttendanceTable.Columns.Add(new DataColumn("Group Name", typeof(string)));
                retreatAttendanceTable.Columns.Add(new DataColumn("Room Type", typeof(string)));
                retreatAttendanceTable.Columns.Add(new DataColumn("Room Assignment", typeof(string)));
                retreatAttendanceTable.Columns.Add(new DataColumn("First Floor Req", typeof(string)));
                retreatAttendanceTable.Columns.Add(new DataColumn("Dietary Needs", typeof(string)));
                retreatAttendanceTable.Columns.Add(new DataColumn("Comment", typeof(string)));
                retreatAttendanceTable.Columns.Add(new DataColumn("Amount Paid", typeof(string)));
                retreatAttendanceTable.Columns.Add(new DataColumn("Discount Amount", typeof(string)));

                RetreatRegistrationUtility retreatRegistrationUtility = new RetreatRegistrationUtility();
                List<RetreatRegistrationInfo> registrationList = null;

                ConversionUtility converter = new ConversionUtility();
                
                if (Request.QueryString.Count == 0)
                {   // get all active scheduled retreats
                    registrationList = retreatRegistrationUtility.getAllRetreatAttendance();
                }
                else
                {
                    int scheduleId = converter.stringToInt(Request.QueryString["ID"]);
                    // get active scheduled retreats matching ids of csv list of parameters
                    registrationList = retreatRegistrationUtility.getRetreatAttendanceByScheduleId(scheduleId);
                }

                foreach (RetreatRegistrationInfo registration in registrationList)
                {
                    tableRow = retreatAttendanceTable.NewRow();
                    tableRow["Retreat Title"] = registration.retreatTitle;
                    tableRow["Attendee Name"] = registration.attendeeName;
                    tableRow["Group Name"] = registration.groupName;
                    tableRow["Room Type"] = registration.roomType;
                    tableRow["Room Assignment"] = registration.roomAssignment;
                    tableRow["First Floor Req"] = converter.boolToYesNo(registration.firstFloorRequired);
                    tableRow["Dietary Needs"] = registration.dietaryNeeds;
                    tableRow["Comment"] = registration.comment;
                    tableRow["Amount Paid"] = registration.amountPaid;
                    tableRow["Discount Amount"] = registration.discountAmount;

                    retreatAttendanceTable.Rows.Add(tableRow);

                    // this will be used on edit page so schedule can be prepopulated
                    string rowIndexRegistrationIdXref = String.Format("rowIndexRegistrationIdXref_{0}", retreatAttendanceTable.Rows.Count);
                    Session[rowIndexRegistrationIdXref] = registration.attendanceId;
                }

                ViewState["CurrentTable"] = retreatAttendanceTable;

                GridView1.DataSource = retreatAttendanceTable;
                GridView1.DataBind();
            }

        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            // get schedule id from session varibles saved in load complete
            int xrefId;

            if (GridView1.SelectedIndex < 0)
            {   // selected index < 0 when clicking select/edit on 1st row
                xrefId = 1;
            }
            else
            {
                xrefId = GridView1.SelectedIndex + 1;
            }

            string sessionVariable = String.Format("rowIndexRegistrationIdXref_{0}", xrefId);
//            Response.Redirect("~/RetreatRegistration.aspx?ID=" + Session[sessionVariable].ToString());

            string redirectPage = String.Format("~/RetreatRegistration.aspx?ID={0}", Session[sessionVariable].ToString());
            Response.Redirect(redirectPage);
        }
    }
}